package com.kanq.service.designpatterns.proxy;

import java.io.*;
import java.lang.reflect.Method;

/**
 * @Author cheng jun
 * @Description: 代理模式：静态代理
 * @Date 2021/7/14 20:24
 */
public class People implements PeopleInterface {
    @Override
    public void move() {
        System.out.println("走路------------------");
    }

    public static void main(String[] args) {
        new PeopleTimeProxy(
                new PeopleLogsProxy(
                        new People()
                )
        ).move();
        System.out.println("------------------------------");
        new PeopleLogsProxy(
                new PeopleTimeProxy(
                        new People()
                )
        ).move();
    }
}

interface PeopleInterface {
    void move();
}

/**
 * 时间代理
 */
class PeopleTimeProxy implements PeopleInterface {

    PeopleInterface peopleInterface;

    public PeopleTimeProxy(PeopleInterface peopleInterface) {
        this.peopleInterface = peopleInterface;
    }

    @Override
    public void move() {
        long startTime = System.currentTimeMillis();
        peopleInterface.move();
        long endTime = System.currentTimeMillis();
        System.out.println(endTime - startTime);
    }
}

/**
 * 日志代理
 */
class PeopleLogsProxy implements PeopleInterface {

    PeopleInterface peopleInterface;

    public PeopleLogsProxy(PeopleInterface peopleInterface) {
        this.peopleInterface = peopleInterface;
    }

    @Override
    public void move() {
        System.out.println("logs start-------------------------");
        peopleInterface.move();
        System.out.println("logs end---------------------------");
    }
}
